package com.javasea.business.service;

import com.codingapi.txlcn.tc.annotation.LcnTransaction;
import com.codingapi.txlcn.tc.annotation.TxTransaction;
import com.javasea.business.feign.OrderFeignClient;
import com.javasea.business.feign.StorageFeignClient;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;

@Slf4j
@Service
public class BusinessService {

    @Resource
    private StorageFeignClient storageFeignClient;
    @Resource
    private OrderFeignClient orderFeignClient;

    @Autowired
    private Business2Service business2Service;

    /**
     * 减库存，下订单
     * @param userId
     * @param commodityCode
     * @param orderCount
     */
    @Transactional(rollbackFor = Exception.class)
    public void purchase(String userId, String commodityCode, int orderCount) {
        // 减库存
        storageFeignClient.deduct(commodityCode, orderCount);
        // 创建订单
        orderFeignClient.create(userId, commodityCode, orderCount);
    }

    @LcnTransaction
    @Transactional(rollbackFor = Exception.class)
    public void purchase2(String userId, String commodityCode, int orderCount) {
        business2Service.purchase(userId, commodityCode, orderCount);
    }

}
